home *** CD-ROM | disk | FTP | other *** search
- Path: news.mtu.edu!not-for-mail
- From: sjang@mtu.edu (Saurabh Jang)
- Newsgroups: comp.lang.c
- Subject: Re: Matrix Multiplication
- Date: 27 Jan 1996 02:24:20 -0500
- Organization: Michigan Technological University
- Message-ID: <4ecjv4$fn2@tamarack.cs.mtu.edu>
- References: <1996Jan22.110440@gamma.ntu.ac.sg>
- NNTP-Posting-Host: tamarack.cs.mtu.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Long (sz7212510@gamma.ntu.ac.sg) wrote:
- : Dear all,
-
- : I would like to know whether anyone of you out there has better ways to
- : do matrix multiplication (optimised for speed) in C either than using
- : two nested for loops.
-
- : Thanks.
-
- : regds
- : Jos
-
- Forget any rubbish such as Strassen's algorithm or Knuth's algorithms.
- these have very large constants and will buy you any thing only if
- you are dealing with HUGE matrices.
-
- Consider optimizing for cache performance.
-
- for ( i = 0; i < N; i++ )
- for ( j = 0; j < N; j++ )
- {
- c[i][j] = 0.0;
-
- for ( k = 0; k < N; k++ )
- {
- c[i][j] = c[i][j] + a[i][k]*b[k][j];
- }
- }
-
- increase in performance. Remember to initialize all the elems of c
- that you access in the innermost loop to zero before the innermost
- loop.
-
- The reason for the improved performance lies in the reuse you get in
- the data cache from different loop orderings.
-
- --
- Saurabh Jang e-mail: sjang@cs.mtu.edu
- MSCS Student www : http://www.cs.mtu.edu/grads/Jang/Home.html
- Michigan Tech work #: (906)487-2839
-